<script setup lang="ts">
import { LEVELOPTIONS } from '@/constant/options'
import type { FormRules } from 'element-plus'
import { onMounted, reactive, ref } from 'vue'

interface Props {
  data?: Record<string, any> | never | null
  status?: string | never | null
}

const { data = null } = defineProps<Props>()

const baseFormRef = ref()

const form = ref<Record<string, any>>({
  level: '0',
})

const rules = reactive<FormRules>({
  title: [{ required: true, message: '标题不能为空!', trigger: 'blur' }],
  level: [{ required: true, message: '工单等级不能为空!', trigger: 'blur' }],
  content: [{ required: true, message: '内容不能为空!', trigger: 'blur' }],
})

const levelOptions = ref(LEVELOPTIONS)

const initForm = () => {
  if (data) {
    Object.keys(data).forEach((key: string) => {
      if (key == 'roles') {
        form.value.roleid = data![key][0].id
      } else {
        form.value[key] = data![key]
      }
    })
  }
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form.value).forEach((key: string) => {
    result[key] = form.value[key]
  })
  return result
}

const submitForm = () => {
  return baseFormRef.value.submit(formatForm)
}

defineExpose({
  submitForm,
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <BaseForm ref="baseFormRef" :form="form" :rules="rules" :attributes="{ labelWidth: '90px' }">
    <el-form-item label="工单级别:" prop="level">
      <el-select v-model="form.level" placeholder="工单级别" style="width: 120px">
        <el-option
          v-for="item in levelOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="标题:" prop="title">
      <el-input
        v-model="form.title"
        type="text"
        placeholder="请输入内容"
        maxlength="30"
        show-word-limit
      >
      </el-input>
    </el-form-item>
    <el-form-item label="内容:" prop="content">
      <el-input
        ref="textareaRef"
        v-model="form.content"
        type="textarea"
        :rows="3"
        maxlength="200"
        placeholder="输入工单内容"
        show-word-limit
      />
    </el-form-item>
  </BaseForm>
</template>

<style lang="scss" scoped></style>
